(** The main APython file, bootstraps the compiler *)

open Environment;;
open Lexing;;

(* Apparently OCaml main functions are called underscore, and take no args. Good to know *)
let _ =
    let filename = ref "-" in       (* Default to stdin *)
    let outname = ref "out.ap" in   (* Default to out.ap - a bit like gcc *)
    Arg.parse [("-o", Arg.String (fun x -> outname := x), 
            ("filename\tSpecify the output file (- denotes stdout). "^
            "If this is not specified then out.ap is used")) ]
        (fun f -> filename := f) 
        ("Usage: apython [sourcefile]\n" ^
        "If no sourcefile is given (or sourcefile is -), code is read from stdin.");
    let code_channel = ref stdin in
    if !filename <> "-" then code_channel := open_in !filename;   (* If a file is given, use it *)
    (* Parse the input file *)
    let apy_mod = 
        (Modules.load_builtins "./") @ (Modules.load_from_channel !code_channel !filename) in
    let out_channel = ref stdout in
    if !outname <> "-" then out_channel := open_out !outname;
    (* And generate code: *)
    List.iter (Codegen.gen_stmt !out_channel) apy_mod
;;
